package com.goldgov.synchronous.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.meetingstatistics.bean.MeetingParamsBean;
import com.goldgov.synchronous.dao.ConferenceSynchronousDao;
import com.goldgov.synchronous.service.ConferenceSynchronous;
import com.goldgov.synchronous.service.ConferenceSynchronousService;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;

/** 
 * @ClassName: ConferenceSynchronousServiceImpl 
 * @Description: TODO
 * @author hanxiangyu
 * @date 2018年10月19日 下午2:14:31 
 */
@Service
public class ConferenceSynchronousServiceImpl implements ConferenceSynchronousService {

	
	@Autowired
	private ConferenceSynchronousDao conferenceSynchronousDao;
	/** 
	 * @Description: 三会一课调度执行逻辑接口
	 * @author hanxiangyu
	 * @date 2018年10月19日 下午2:14:31 
	 */
	@Override
	public synchronized void conferenceSynchronousStoreBasicData() throws Exception{
		MeetingParamsBean meetingParamsBean = new MeetingParamsBean();
		meetingParamsBean.setStaticYear(DateUtil.getYear(new Date()));
		int month = DateUtil.getMonth(new Date());
		for (int i = 1; i <= month ; i++) {
			meetingParamsBean.setSearchMonth(i);
			//查询当前年i月的数据
			List<ConferenceSynchronous> list = conferenceSynchronousDao.findConTypeNumByYearOrMonth(meetingParamsBean);
			//查远程教育
			List<ConferenceSynchronous> list2 = conferenceSynchronousDao.findDistanceLearningNumByYearOrMonth(meetingParamsBean);
			list.addAll(list2);
			String strDate = meetingParamsBean.getStaticYear().toString();
			if (i < 10) {
				strDate += "0" + i; 
			}else {
				strDate += i; 
			}
			Date date = DateUtil.strToDate(strDate); //转换为时间
			for (ConferenceSynchronous conferenceSynchronous : list) {
				//查询是否存在此记录
				ConferenceSynchronous synchronous = conferenceSynchronousDao.findConSynchronous(conferenceSynchronous.getOrganizationId(), date);
				if (PropertyUtil.objectNotEmpty(synchronous)) { //存在进行更新
					conferenceSynchronousDao.updateConSynchronous(conferenceSynchronous);
				}else { //不存在进行插入
					conferenceSynchronous.setStatisticsDate(date);
					try {
						conferenceSynchronousDao.addConSynchronous(conferenceSynchronous);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			
			
		}
		
		
	}
}
